package ws

import (
	"net/http"
	"sync"
	"time"

	"github.com/gorilla/websocket"
	"github.com/labstack/echo/v4"
)

type Admin struct {
	Conn       *websocket.Conn
	Name       string
	Id         string
	Avator     string
	To_id      string
	Role_id    string
	Mux        sync.Mutex
	UpdateTime time.Time
}

type TypeMessage struct {
	Type interface{} `json:"type"`
	Data interface{} `json:"data"`
}

type Message struct {
	conn        *websocket.Conn
	context     echo.Context
	content     []byte
	messageType int
	Mux         sync.Mutex
}

var upgrader = websocket.Upgrader{}
var message = make(chan *Message, 10)
var KefuList = make(map[string][]*Admin)

func init() {
	upgrader = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		// 解决跨域问题
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
}

func WsServerBackend() {
	for {
		message := <-message
		conn := message.conn

		message.Mux.Lock()
		conn.WriteMessage(message.messageType, message.content)
		message.Mux.Unlock()
	}

}
